var toolUtil = require('./toolUtil');
var mysqlUtil = require('../../common/utils/mysqlUtil');

exports.composeErrorMessage = function (req, errMessage) {
    //toolUtil.doAudit(req, 'failed');
    return {result: errMessage, data: null}
};


function synchronize(req) {

    //logger.debug(req.headers['x-real-ip']);
    //logger.debug(req.headers);

    var body = {};
    if (req.path) body.path = req.path;
    if (req.ip) body.ip = req.headers['x-real-ip'];
    if (req.session.account && req.session.account.userID) body.userID = req.session.account.userID;
    if (req.session.account && req.session.account.username) body.username = req.session.account.username;

    logger.debug('操作日志:' + body.path + ' ' + body.ip + ' ' + body.userID + ' ' + body.username);
    var sql = "insert into sys_log set ?";
    mysqlUtil.query(sql, [body],function (err, result) {
        //callback(err, result);
    });
};


// /**
// * 构造失败消息
// */
exports.composeFailedMessage = function (req, errMsg) {

    //toolUtil.doAudit(req, 'failed');

    synchronize(req);

    return returnMessage = {
        "result": {
            "status": 0,
            "message": errMsg
        },
        "data": null
    }
};


// /**
// *构造成功消息
// */
exports.composeSucceededMessage = function (req, msg, data) {

    if (msg == null || msg == '') {
        msg = 'Ok';
    }

    //toolUtil.doAudit(req, 'success');

    synchronize(req);

    return returnMessage = {
        "result": {
            "status": 1,
            "message": msg
        },
        "data": JSON.parse(JSON.stringify(data).replace(/"_id"/g, '"objId"'))
    }
}

/**
 * 构造android推送消息
 */

exports.composeAndroidPushMessage = function (description) {
    var message = {
        "description": description,
        "notification_basic_style": 0x01,
        "open_type": 2
    };
    return message;
};